/*
 * @Author: chunlai
 * @Date: 2025-07-18 13:50:10
 * @LastEditTime: 2025-08-19 11:19:09
 * @LastEditors: minsheng_insurance_agency_zhoucl
 * @FilePath: \02.bldinsure\src\mixins\billdate.js
 * @Description: 保单生效日期
 */
import { mapGetters } from "vuex";
import { dateToString } from "@/methods/date.js";

export const billDateMixin = {
  data() {
    return {
      showPicker: false,
      endPicker: false,
      minDate: "",
      maxEndDate: new Date("2099/12/31"),
    };
  },

  computed: {
    ...mapGetters({
      insu2Year: "page/insuYear",
    }),
    maxDate() {
      if (this.minDate && this.insu2Year.maxDate > 1) {
        const start = this.minDate.getTime(),
          maxGut = Number(this.insu2Year.maxDate) * 24 * 60 * 60 * 1000;
        const val = start + maxGut;
        return new Date(val);
      }
      return new Date();
    },
  },
  methods: {
    initInsureDate() {
      const now = new Date().getTime(),
        minGut = Number(this.insu2Year.minDate || 1);
      const startValue = dateToString(
        new Date(now + minGut * 24 * 60 * 60 * 1000)
      );
      this.minDate = new Date(startValue);
      if (!this.orderInfo.effectiveDate) {
        this.changeData({ effectiveDate: startValue });
      }
      if (
        this.infoReg.hasOwnProperty("expiryDate") &&
        !this.orderInfo.expiryDate
      ) {
        this.toSetEnd(startValue);
      }
    },
    showStartPicker() {
      // 可以选择保险止期
      if (this.insu2Year.maxDate > 1) {
        this.showPicker = true;
      }
    },
    showEndPicker() {
      // 可以选择保险止期
      if (this.insu2Year?.endType != "text") {
        if (this.orderInfo.expiryDate) {
          this.maxEndDate = new Date(
            this.orderInfo.expiryDate.replace(/-/g, "/")
          );
        }
        this.endPicker = true;
      }
    },
    toSetEnd(val) {
      /**自动设置止期*/
      const date = new Date(val.replace(/-/g, "/")),
        gutValue = this.insu2Year?.gutterYear || 1,
        year = `${date.getFullYear() + gutValue}`,
        month = `${date.getMonth() + 1}`,
        day = `${date.getDate()}`,
        endDate2 = new Date(`${year}/${month}/${day}`);
      const endStr = dateToString(endDate2.getTime() - 24 * 60 * 60 * 1000);
      this.changeData({ expiryDate: endStr });
    },
    onBeginConfirm(date) {
      const val = dateToString(date);
      this.changeData({ effectiveDate: val });
      if (this.infoReg.hasOwnProperty("expiryDate")) {
        this.toSetEnd(val);
      }
      this.showPicker = false;
    },
    onEndConfirm(date) {
      const val = dateToString(date);
      this.changeData({ expiryDate: val });
      this.endPicker = false;
    },
  },
};
